package com.xm.service.impl;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.xm.context.BaseContext;
import com.xm.domain.dto.CategoryDTO;
import com.xm.domain.dto.CategoryPageQueryDTO;
import com.xm.domain.po.Category;
import com.xm.domain.po.Employee;
import com.xm.domain.vo.CategoryVO;
import com.xm.mapper.CategoryMapper;
import com.xm.result.PageResult;
import com.xm.service.CategoryService;
import com.xm.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Override
    public void saveCategory(CategoryDTO categoryDTO) {
        //1 处理数据
        Category category = BeanUtils.copyBean(categoryDTO, Category.class);
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateTime(LocalDateTime.now());
        category.setStatus(0);

        //1.1 拿到当前商家id,去找到店铺
        Long currentId = BaseContext.getCurrentId();
        Employee employee = Db.lambdaQuery(Employee.class).eq(Employee::getId, currentId).one();
        String shopId = employee.getShopId();

        category.setCreateUser(currentId);
        category.setUpdateUser(currentId);

        category.setShopId(shopId);
        // 2插入分类
        save(category);

    }

    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //1构建分页条件
        //1.1 分页条件
        Page<Category> page = Page.of(categoryPageQueryDTO.getPageNo(), categoryPageQueryDTO.getPageSize());
        //1.2 排序条件 基本默认根据修改时间降序
        page.addOrder( OrderItem.desc("update_time"));

        //2.分页查询
        //2.1 拿到当前商家id,去找到店铺

        Long currentId = BaseContext.getCurrentId();
        Employee employee = Db.lambdaQuery(Employee.class).eq(Employee::getId, currentId).one();
        String shopId = employee.getShopId();

        Page<Category> p= lambdaQuery()
                .like( categoryPageQueryDTO.getName() != null, Category::getName,  categoryPageQueryDTO.getName())
                .eq(categoryPageQueryDTO.getType()!=null,Category::getType,categoryPageQueryDTO.getType())
                .eq(Category::getShopId,shopId)
                .page(page);
        //3封装PageResult结果
        PageResult<CategoryVO> pageResult = new PageResult<>();
        //3.1 总条数
        pageResult.setTotal(   page.getTotal());

        //3.2 总页数
        pageResult.setPages(page.getSize());
        //3.3 数据
        List<Category> records = p.getRecords();
        List<CategoryVO> categories = BeanUtils.copyList(records, CategoryVO.class);

        pageResult.setList(categories);
        return pageResult;
    }
}
